package com.yyk.yykywd;

import android.app.Dialog;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Handler;
import android.os.Message;
import android.text.method.ScrollingMovementMethod;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;

import androidx.core.app.NotificationCompat;
import androidx.core.content.FileProvider;

import com.orhanobut.logger.Logger;
import com.yyk.yykywd.config.HttpUrl;
import com.yyk.yykywd.http.ApiService;
import com.yyk.yykywd.listenter.DialogSureCancelListenter;
import com.yyk.yykywd.mvvm.entity.APPInfo;
import com.yyk.yykywd.ui.MainActivity;
import com.yyk.yykywd.util.DialogUtil;
import com.yyk.yykywd.util.FileUtils;
import com.yyk.yykywd.util.Md5Util2;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Map;
import java.util.TreeMap;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * 版本更新工具类
 * 具体需求：进入app请求接口，如果服务器versionCode大于apk的versionCode，提示用户更新
 * 用户点击立即更新，下载apk，在notification中显示下载进度，下载完成之后，弹框提示用户去安装
 * 同时也可以点击notification进行安装
 */

public class CheckUpdateUtils {
    /**
     * 更新dialog
     */
    public static Dialog showUpdateDialog(Context context,final APPInfo updateInfo,ClickUpdate clickUpdate) {
        View view = LayoutInflater.from(context).inflate(R.layout.dialog_update, null);
        //此处直接new一个Dialog对象出来，在实例化的时候传入主题
        Dialog updateDialog = new Dialog(context, R.style.custom_dialog);
        //设置它的ContentView
        updateDialog.setContentView(view);

        TextView tvContent = (TextView) updateDialog
                .findViewById(R.id.update_content);
        tvContent.setMovementMethod(ScrollingMovementMethod.getInstance());
        final Button bt_cancle = (Button) updateDialog.findViewById(R.id.bt_cancle);
        final Button bt_ok = (Button) updateDialog.findViewById(R.id.bt_ok);
        TextView tv_version = (TextView) updateDialog.findViewById(R.id.tv_version);


        tv_version.setText(updateInfo.getData().getVersion());

        //宽度设置为屏幕的0.8，根据实际情况调整
        WindowManager.LayoutParams params = updateDialog.getWindow().getAttributes();
        params.width = (int) (context.getResources().getDisplayMetrics().widthPixels * 0.70);
        updateDialog.getWindow().setAttributes(params);

        tvContent.setText(updateInfo.getData().getContent().replace("\\n", "\n"));
        if (updateInfo.getData().getFlag() == 1) {//是否强制更新
            updateDialog.setCancelable(false);
            bt_cancle.setVisibility(View.INVISIBLE);
        } else {
            bt_cancle.setVisibility(View.VISIBLE);
        }
        bt_cancle.setOnClickListener(arg0 -> updateDialog.dismiss());
        bt_ok.setOnClickListener(arg0 -> clickUpdate.click());
        updateDialog.show();

        return updateDialog;
    }

    public interface ClickUpdate {
        void click();
    }
}
